# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(tidyverse)
library(estimatr)
library(metafor)
library(broom)
library(coefplot)
library(reshape2)
library(xtable)

source("code/helpers.R")
treatments_df <- read_rds("data/treatments_df.rds")

ates_w1_all_adj <- read_rds("fitted_models/ates_w1_all_adj.rds")
ates_w1_all_noadj <- read_rds("fitted_models/ates_w1_all_noadj.rds")
cates_w1_all_noadj <- read_rds("fitted_models/cates_w1_all_noadj.rds")
cates_w1_all_adj <- read_rds("fitted_models/cates_w1_all_adj.rds")
ates_w1_p2_noadj <- read_rds("fitted_models/ates_w1_p2_noadj.rds")
ates_w1_p2_adj <- read_rds("fitted_models/ates_w1_p2_adj.rds")
ates_w2_p2_noadj <- read_rds("fitted_models/ates_w2_p2_noadj.rds")
ates_w2_p2_adj <- read_rds("fitted_models/ates_w2_p2_adj.rds")
iv_p2_adj <- read_rds("fitted_models/iv_p2_adj.rds")
iv_p2_noadj <- read_rds("fitted_models/iv_p2_noadj.rds")
ates_w1_p3_noadj <- read_rds("fitted_models/ates_w1_p3_noadj.rds")
ates_w1_p3_adj <- read_rds("fitted_models/ates_w1_p3_adj.rds")
ates_w3_p3_noadj <- read_rds("fitted_models/ates_w3_p3_noadj.rds")
ates_w3_p3_adj <- read_rds("fitted_models/ates_w3_p3_adj.rds")
iv_p3_adj <- read_rds("fitted_models/iv_p3_adj.rds")
iv_p3_noadj <- read_rds("fitted_models/iv_p3_noadj.rds")


reshape_ates <-
  function(data){
    data %>%
      filter(str_detect(string = term, "treatment|outcome")) %>%
      melt(id.vars = c("fc", "panel", "panel_factor", "term", "outcome")) %>%
      dcast(outcome + fc + panel + panel_factor~ term + variable)  %>%
      arrange(panel_factor, fc) %>%
      left_join(select(treatments_df, topic_short, panel, fc)) %>%
      transmute(
        topic_short,
        panel_factor,
        `Average Effect of Misinformation versus Control` = paste0(
          make_se_entry(treatmentcontrol_estimate, treatmentcontrol_std.error),
          if_else(treatmentcontrol_p.value < 0.05, "* ", " "),
          make_interval_entry(treatmentcontrol_conf.low, treatmentcontrol_conf.high)
        ),
        `Average Effect of Correction versus Misinformation` = paste0(
          make_se_entry(treatmentfactcheck_estimate, treatmentfactcheck_std.error),
          if_else(treatmentfactcheck_p.value < 0.05, "* ", " "),
          make_interval_entry(treatmentfactcheck_conf.low, treatmentfactcheck_conf.high)
        )
      )
  }

reshape_cates <-
  function(data){
    data %>%
      filter(str_detect(string = term, "treatment|outcome")) %>%
      melt(id.vars = c("fc", "panel", "panel_factor", "term", "outcome", "lucid_pid_3")) %>%
      dcast(outcome + fc + panel + panel_factor + lucid_pid_3 ~ term + variable)  %>%
      arrange(panel_factor, fc, lucid_pid_3) %>%
      left_join(select(treatments_df, topic_short, panel, fc)) %>%
      transmute(
        topic_short,
        panel_factor,
        lucid_pid_3,
        `Average Effect of Misinformation versus Control` = paste0(
          make_se_entry(treatmentcontrol_estimate, treatmentcontrol_std.error),
          if_else(treatmentcontrol_p.value < 0.05, "* ", " "),
          make_interval_entry(treatmentcontrol_conf.low, treatmentcontrol_conf.high)
        ),
        `Average Effect of Correction versus Misinformation` = paste0(
          make_se_entry(treatmentfactcheck_estimate, treatmentfactcheck_std.error),
          if_else(treatmentfactcheck_p.value < 0.05, "* ", " "),
          make_interval_entry(treatmentfactcheck_conf.low, treatmentfactcheck_conf.high)
        )
      )
  }

reshape_persistence <-
  function(data) {
    data %>%
      filter(str_detect(string = term, "treatment|outcome")) %>%
      left_join(select(treatments_df, topic_short, panel, fc)) %>%
      transmute(
        topic_short,
        panel_factor,
        `Average Effect of Correction versus Control` = paste0(
          make_se_entry(estimate, std.error),
          if_else(p.value < 0.05, "* ", " "),
          make_interval_entry(conf.low, conf.high)
        )
      )
  }



# Ates wave 1 -------------------------------------------------------



ates_w1_all_noadj_meta <-
  ates_w1_all_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-term2, -type)

ates_w1_all_adj_meta <-
  ates_w1_all_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-term2, -type)
  
  
ate_w1_table <-
  bind_rows(
    ates_w1_all_noadj %>% reshape_ates() %>% mutate(covariates = "no"),
    ates_w1_all_noadj_meta %>% reshape_ates() %>% mutate(covariates = "no", topic_short = "Meta-analysis"),
    ates_w1_all_adj %>% reshape_ates() %>% mutate(covariates = "yes"),
    ates_w1_all_adj_meta %>% reshape_ates() %>% mutate(covariates = "yes", topic_short = "Meta-analysis"),
  ) %>%
  transmute(
    topic_short,
    panel_factor,
    covariates,
    `Average Effect of Misinformation versus Control`,
    `Average Effect of Correction versus Misinformation`
  ) %>%
  mutate(panel_factor = factor(panel_factor),
         panel_factor = relevel(panel_factor, ref = "Meta-analysis"),
         panel_factor = factor(panel_factor, levels = rev(levels(panel_factor)))) %>%
  arrange(panel_factor, topic_short, covariates)


xtable(ate_w1_table) %>%
  print(include.rownames = FALSE,
        include.colnames = FALSE,
        only.contents = TRUE,
        hline.after = c()) 



# Cates w1 ----------------------------------------------------------

cates_w1_all_noadj_meta <-
  cates_w1_all_noadj %>%
  mutate(term2 = term) %>%
  group_by(lucid_pid_3,term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-term2, -type)

cates_w1_all_adj_meta <-
  cates_w1_all_adj %>%
  mutate(term2 = term) %>%
  group_by(lucid_pid_3,term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-term2, -type)


cate_w1_table <-
  bind_rows(
    cates_w1_all_noadj %>% reshape_cates() %>% mutate(covariates = "no"),
    cates_w1_all_noadj_meta %>% reshape_cates() %>% mutate(covariates = "no", topic_short = "Meta-analysis"),
    cates_w1_all_adj %>% reshape_cates() %>% mutate(covariates = "yes"),
    cates_w1_all_adj_meta %>% reshape_cates() %>% mutate(covariates = "yes", topic_short = "Meta-analysis"),
  ) %>%
  transmute(
    topic_short,
    panel_factor,
    lucid_pid_3,
    covariates,
    `Average Effect of Misinformation versus Control`,
    `Average Effect of Correction versus Misinformation`
  ) %>%
  mutate(panel_factor = factor(panel_factor),
         panel_factor = relevel(panel_factor, ref = "Meta-analysis"),
         panel_factor = factor(panel_factor, levels = rev(levels(panel_factor)))) %>%
  arrange(panel_factor, topic_short, covariates, lucid_pid_3)

cate_w1_table %>%
  filter(lucid_pid_3 == "Democrat") %>%
  xtable() %>%
  print(
    include.rownames = FALSE,
    include.colnames = FALSE,
    only.contents = TRUE,
    hline.after = c()
  )


cate_w1_table %>%
  filter(lucid_pid_3 == "Republican") %>%
  xtable() %>%
  print(
    include.rownames = FALSE,
    include.colnames = FALSE,
    only.contents = TRUE,
    hline.after = c()
  )


# persistence_p2 ----------------------------------------------------

ates_w1_p2_noadj_meta <-
  ates_w1_p2_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-type)

ates_w1_p2_adj_meta <-
  ates_w1_p2_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-type)

ates_w2_p2_noadj_meta <-
  ates_w2_p2_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w2"
  ) %>%
  select(-type)

ates_w2_p2_adj_meta <-
  ates_w2_p2_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w2"
  ) %>%
  select(-type)


iv_p2_noadj_meta <-
  iv_p2_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>% 
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w2",
  ) %>%
  select(-type)


iv_p2_adj_meta <-
  iv_p2_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w2",
    term = "outcome"
  ) %>%
  select(-type)


persistence_p2_table <-
  bind_rows(
    ates_w1_p2_noadj %>% reshape_persistence() %>% mutate(covariates = "no", wave = "w1"),
    ates_w1_p2_adj %>% reshape_persistence() %>% mutate(covariates = "yes", wave = "w1"),
    ates_w2_p2_noadj %>% reshape_persistence() %>% mutate(covariates = "no", wave = "w2"),
    ates_w2_p2_adj %>% reshape_persistence() %>% mutate(covariates = "yes", wave = "w2"),
    iv_p2_noadj %>% reshape_persistence %>% mutate(covariates = "no", wave = "ratio"),
    iv_p2_adj %>% reshape_persistence %>% mutate(covariates = "yes", wave = "ratio"),
    
    ates_w1_p2_noadj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "no", wave = "w1"),
    ates_w1_p2_adj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "yes", wave = "w1"),
    ates_w2_p2_noadj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "no", wave = "w2"),
    ates_w2_p2_adj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "yes", wave = "w2"),
    iv_p2_noadj_meta %>% reshape_persistence %>% mutate(topic_short = "Meta-analysis", covariates = "no", wave = "ratio"),
    iv_p2_adj_meta %>% reshape_persistence %>% mutate(topic_short = "Meta-analysis", covariates = "yes", wave = "ratio")
  ) %>%
  ungroup %>%
  mutate(wave = factor(wave, c("w1", "w2", "ratio")),
         panel_factor = factor(panel_factor),
         panel_factor = relevel(panel_factor, ref = "Meta-analysis"),
         panel_factor = factor(panel_factor, levels = rev(levels(panel_factor)))) %>%
  transmute(
    topic_short,
    panel_factor,
    wave,
    covariates,
    `Average Effect of Correction versus Control`
  ) %>%
  arrange(panel_factor, topic_short, covariates, wave)


persistence_p2_table %>%
  filter(covariates == "no") %>%
  xtable() %>%
  print(
    include.rownames = FALSE,
    include.colnames = FALSE,
    only.contents = TRUE,
    hline.after = c()
  )

persistence_p2_table %>%
  filter(covariates == "yes") %>%
  xtable() %>%
  print(
    include.rownames = FALSE,
    include.colnames = FALSE,
    only.contents = TRUE,
    hline.after = c())



# persistence_p3 ----------------------------------------------------


ates_w1_p3_noadj_meta <-
  ates_w1_p3_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-type)

ates_w1_p3_adj_meta <-
  ates_w1_p3_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w1"
  ) %>%
  select(-type)

ates_w3_p3_noadj_meta <-
  ates_w3_p3_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w3"
  ) %>%
  select(-type)

ates_w3_p3_adj_meta <-
  ates_w3_p3_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w3"
  ) %>%
  select(-type)


iv_p3_noadj_meta <-
  iv_p3_noadj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>% 
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w3",
  ) %>%
  select(-type)


iv_p3_adj_meta <-
  iv_p3_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  summarise(tidy(
    rma.uni(yi = estimate, sei = std.error, data = cur_data()),
    conf.int = TRUE
  )) %>%
  mutate(
    term = term2,
    panel_factor = "Meta-analysis",
    fc = "Meta-analysis",
    panel = "Meta-analysis",
    outcome = "outcome_w3",
    term = "outcome"
  ) %>%
  select(-type)



persistence_p3_table <-
  bind_rows(
    ates_w1_p3_noadj %>% reshape_persistence() %>% mutate(covariates = "no", wave = "w1"),
    ates_w1_p3_adj %>% reshape_persistence() %>% mutate(covariates = "yes", wave = "w1"),
    ates_w3_p3_noadj %>% reshape_persistence() %>% mutate(covariates = "no", wave = "w3"),
    ates_w3_p3_adj %>% reshape_persistence() %>% mutate(covariates = "yes", wave = "w3"),
    iv_p3_noadj %>% reshape_persistence %>% mutate(covariates = "no", wave = "ratio"),
    iv_p3_adj %>% reshape_persistence %>% mutate(covariates = "yes", wave = "ratio"),
    
    ates_w1_p3_noadj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "no", wave = "w1"),
    ates_w1_p3_adj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "yes", wave = "w1"),
    ates_w3_p3_noadj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "no", wave = "w3"),
    ates_w3_p3_adj_meta %>% reshape_persistence() %>% mutate(topic_short = "Meta-analysis", covariates = "yes", wave = "w3"),
    iv_p3_noadj_meta %>% reshape_persistence %>% mutate(topic_short = "Meta-analysis", covariates = "no", wave = "ratio"),
    iv_p3_adj_meta %>% reshape_persistence %>% mutate(topic_short = "Meta-analysis", covariates = "yes", wave = "ratio")
  ) %>%
  ungroup %>%
  mutate(wave = factor(wave, c("w1", "w2", "ratio")),
         panel_factor = factor(panel_factor),
         panel_factor = relevel(panel_factor, ref = "Meta-analysis"),
         panel_factor = factor(panel_factor, levels = rev(levels(panel_factor)))) %>%
  transmute(
    topic_short,
    panel_factor,
    wave,
    covariates,
    `Average Effect of Correction versus Control`
  ) %>%
  arrange(panel_factor, topic_short, covariates, wave)


persistence_p3_table %>%
  filter(covariates == "no") %>%
  xtable() %>%
  print(
    include.rownames = FALSE,
    include.colnames = FALSE,
    only.contents = TRUE,
    hline.after = c()
  )

persistence_p3_table %>%
  filter(covariates == "yes") %>%
  xtable() %>%
  print(
    include.rownames = FALSE,
    include.colnames = FALSE,
    only.contents = TRUE,
    hline.after = c()
  )

